package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.db.utils.RedisTokenUtil;
import com.yt.user.models.bo.UserLoginSaveBO;
import com.yt.user.models.vo.UserInfoVO;
import com.yt.user.models.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.IUserLoginService;
import com.yt.user.models.bo.UserLoginBO;
import com.yt.user.models.dto.UserLoginDTO;

import java.util.HashMap;

/**
 * 用户登录控制器
 *
 * @author lvzeming
 * @Date 2025-08-20 12:13:53
 */

@RestController
@RequestMapping("/api/userLogin")
@Slf4j
@Api(value = "UserLoginApiController",tags = "用户登录Api接口")
public class UserLoginApiController {

    @DubboReference
    private IUserLoginService userLoginService;

    @Autowired
    private RedisTokenUtil redisTokenUtil;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<UserLoginDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserLoginDTO dto = userLoginService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "登录",notes = "登录")
    @PostMapping(value = "/login")
    public Result<HashMap<String,Object>> login(@RequestBody UserLoginSaveBO saveBO) {
        HashMap<String,Object> map =  new HashMap<String,Object>();
        if(null == saveBO){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        //判断账号是否存在
        UserLoginVO userLoginVO = userLoginService.getLogin(saveBO);
        if(null == userLoginVO){
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }

        UserInfoVO userInfoVO = userLoginService.getUserLogin(saveBO);
        if(null == userInfoVO){
            return new Result<>(CodeEnum.FAIL_USER_200006);
        }

        //生成Token并存入到Redis
        String token = redisTokenUtil.generateToken(userInfoVO.getName());
        userInfoVO.setToken(token);


        map.put("userInfoVO",userInfoVO);
        return new Result<>(map);
    }

    @ApiOperation(value = "用户登录列表",notes = "用户登录列表")
    @PostMapping("/queryPageList")
    public Result<Page<UserLoginDTO>> queryPageList(UserLoginBO bo){
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<UserLoginDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = userLoginService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
